package com.token.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import com.alibaba.fastjson.JSON;
import com.token.domain.SystemInfomation;
import com.token.task.VerifyTask;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.apache.commons.lang3.time.DateUtils;

import javax.crypto.SecretKey;
import java.io.File;
import java.util.*;

/**
 * @author chenming
 * @description
 * @create: 2022-08-09
 */
public class JWTUtil {

    public static final String APPLY_KEY = "1127f8a5-d624-4c99-8940-decb96a11b37";
    public static final String DES_KEY = "1127f8a5";

    private static final String property = System.getProperty("user.dir");
    private static final File apply = new File(property + "/apply.lic");
    private static final File license = new File(property + "/license.lic");


    private static List<SignatureAlgorithm> signMethods = new ArrayList<>(Arrays.asList(
            SignatureAlgorithm.HS256,
            SignatureAlgorithm.HS384,
            SignatureAlgorithm.HS512
    ));

    public static boolean verifyToken(String token, String sign) {
        SecretKey key = Keys.hmacShaKeyFor(Base64.encode(sign).getBytes());
        try {
            Jwts.parserBuilder()
                    .setSigningKey(key)
                    .build()
                    .parseClaimsJws(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void updateToken() {
        SecretKey key = Keys.hmacShaKeyFor(Base64.encode(SystemInfoUtil.getSystemInfo().toString()).getBytes());
        Collections.shuffle(signMethods);
        String compact = Jwts.builder()
                .setPayload(JSON.toJSONString(SystemInfoUtil.getSystemInfo()))
                .signWith(key, signMethods.get(0))
                .compact();
        FileWriter writer = new FileWriter(license);
        writer.write(Base64.encode(compact));
    }

    public static boolean applyToken(SystemInfomation systemInfomation) {
        SecretKey key = Keys.hmacShaKeyFor(Base64.encode(APPLY_KEY).getBytes());
        String compact = Jwts.builder()
                .setPayload(JSON.toJSONString(SystemInfoUtil.getSystemInfo()))
                .signWith(key, SignatureAlgorithm.HS256)
                .compact();
        FileWriter writer = new FileWriter(apply);
        writer.write(Base64.encode(compact));
        return true;
    }

    public static void authorizeApply(String applyToken) {
        String str = Base64.decodeStr(applyToken);
        SecretKey key = Keys.hmacShaKeyFor(Base64.encode(APPLY_KEY).getBytes());
        Claims body = Jwts.parserBuilder()
                .setSigningKey(key)
                .build()
                .parseClaimsJws(str).getBody();
        SystemInfomation systemInfomation = JSON.parseObject(JSON.toJSONString(body), SystemInfomation.class);
        SecretKey licenseKey = Keys.hmacShaKeyFor(Base64.encode(systemInfomation.toString()).getBytes());
        String compact = Jwts.builder()
                .setClaims(body)
                .signWith(licenseKey, signMethods.get(0))
                .setExpiration(DateUtils.addYears(new Date(),1))
                .compact();
        FileWriter writer = new FileWriter(license);
        writer.write(Base64.encode(compact));
    }

    public static void main(String[] args) {
        authorizeApply("ZXlKaGJHY2lPaUpJVXpJMU5pSjkuZXlKaVlYTmxRbTloY21SVFRpSTZJazFOUnpWVE1EQXdNREE1T1RreE5FaFFNRGN5VnlJc0ltaGhjbVIzWVhKbFZWVkpSQ0k2SWpjMU1FWkNPRGRGTFVFeE9FVXRNVEZGUWkxR1FURXdMVVpETkRkRVJUTTBNVVpCUVNJc0luTjVjM1JsYlZOT0lqb2lNek0wT0RBdk1ERlJTekF4TlRBMUluMC53MElkWWNqZ3l5UHNMd1ZiR2hHdWY0RXBBbzNOSGlEcTBBTll3SElWWEgw");
    }
}
